package com.example.springbootdemo2023.bus.station.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.example.springbootdemo2023.bus.account.dto.Account;
import com.example.springbootdemo2023.bus.station.dto.Station;
import com.example.springbootdemo2023.bus.station.mapper.StationMapper;
import com.example.springbootdemo2023.bus.station.service.IStationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springbootdemo2023.core.dto.MyPageBand;
import com.example.springbootdemo2023.core.dto.MyPager;
import com.example.springbootdemo2023.core.dto.MyResult;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-04-07
 */
@Service
public class StationServiceImpl extends ServiceImpl<StationMapper, Station> implements IStationService {

    @Override
    public MyResult pages(Station station, MyPageBand page) {
        MyResult result = new MyResult();

        StationMapper stationMapper = getBaseMapper();

        IPage<Station> pages = stationMapper.pages(PageDTO.of(page.getPage(), page.getSize()), station);

        // 根据分页的结果封装返回数据
        MyPager<Station> pager = new MyPager<>();
        pager.setTotal(pages.getTotal());
        pager.setPage(page.getPage());
        pager.setPageSize(page.getSize());
        pager.setRows(pages.getRecords());

        result.setMsg("查询成功！");
        result.setData(pager);

        return result;
    }

    @Override
    public MyResult add(Station station) {
        MyResult result = new MyResult();

        StationMapper stationMapper = getBaseMapper();

        //编号重复查询
        QueryWrapper<Station> queryWrap = new QueryWrapper<>();
        queryWrap.eq("code",station.getCode());

        List<Station> stations = stationMapper.selectList(queryWrap);

        if(stations.size()>0){
            result.setMsg("编号["+station.getCode()+"]重复，请重新换一个编号！");
            result.setCode("9999");
            return result;
        }

        int insert = stationMapper.insert(station);

        if(insert>0){
            result.setMsg("添加成功！");
        }else{
            result.setMsg("添加失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult update(Station station) {
        MyResult result = new MyResult();

        StationMapper stationMapper = getBaseMapper();

        int update = stationMapper.updateById(station);

        if(update>0){
            result.setMsg("修改成功！");
        }else{
            result.setMsg("修改失败！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult delete(String ids) {
        MyResult result = new MyResult();

        List<String> idList = Arrays.asList(ids.split(","));

        StationMapper stationMapper = getBaseMapper();

        QueryWrapper<Station> queryWrapper = new QueryWrapper<>();

        queryWrapper.in("stationID",idList);

        int delete = stationMapper.delete(queryWrapper);

        if(delete>0){
            result.setMsg("删除成功！");
        }else{
            result.setMsg("删除失败！");
            result.setCode("9999");
        }

        return result;
    }
}
